library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Figure 8.5
# Wave 2

summary(factor(df$Q11)) # Confidence
summary(factor(df$Q40)) # recommend mil serv
summary(factor(df$VETERAN))
summary(factor(df$Q37)) # worked/socialized in last month
summary(factor(df$Q38)) # worked/socialized in last year
summary(factor(df$Q39)) # family in military


df$Q11T <- NA
df$Q11T[df$Q11 > 2] <- 0
df$Q11T[df$Q11 < 3] <- 1
summary(factor(df$Q11T))

df$Q40T <- NA
df$Q40T[df$Q40 < 98] <- 0
df$Q40T[df$Q40 == 1] <- 1

df$Q37T <- NA
df$Q37T[df$Q37 < 3] <- 0
df$Q37T[df$Q37 == 1] <- 1

df$Q38T <- NA
df$Q38T[df$Q38 < 3] <- 0
df$Q38T[df$Q38 == 1] <- 1

df$Q39T <- NA
df$Q39T[df$Q39 < 3] <- 0
df$Q39T[df$Q39 == 1] <- 1

df2 <- df[!is.na(df$Q11T),]
df2 <- df2[!is.na(df2$Q40T),]
df2 <- df2[df2$P_ASSIGN1 == 1,]


df2a <- df2[df2$VETERAN<3,]
df2b <- df2[!is.na(df2$Q37T),]
df2c <- df2[!is.na(df2$Q38T),]
df2d <- df2[!is.na(df2$Q39T),]


# Create Survey design
w2_design_a <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2a
  )

w2_design_b <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2b
  )

w2_design_c <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2c
  )

w2_design_d <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2d
  )

# Create plot - vet civilian
v1 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==1 & w2_design_a$variables$VETERAN==1])
v2 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==2 & w2_design_a$variables$VETERAN==1])
v3 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==3 & w2_design_a$variables$VETERAN==1])
v4 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==4 & w2_design_a$variables$VETERAN==1])
gap <- NA
c1 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==1 & w2_design_a$variables$VETERAN==2])
c2 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==2 & w2_design_a$variables$VETERAN==2])
c3 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==3 & w2_design_a$variables$VETERAN==2])
c4 <- mean(w2_design_a$variables$Q40T[w2_design_a$variables$Q11==4 & w2_design_a$variables$VETERAN==2])
l1 <- "A great deal"
l2 <- "Quite a lot"
l3 <- "Some"
l4 <- "Very little"
l5 <- " "
l6 <- " A great deal"
l7 <- " Quite a lot"
l8 <- " Some"
l9 <- " Very little"

ag <- c(l1,l2,l3,l4,l5,l6,l7,l8,l9)
mna <- c(v1,v2,v3,v4,gap,c1,c2,c3,c4)
vetdat <- data.frame(cbind(ag,mna))


# Create plot - Mil in last month
m1 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==1 & w2_design_b$variables$Q37T==1])
m2 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==2 & w2_design_b$variables$Q37T==1])
m3 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==3 & w2_design_b$variables$Q37T==1])
m4 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==4 & w2_design_b$variables$Q37T==1])
gap <- NA
nm1 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==1 & w2_design_b$variables$Q37T==0])
nm2 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==2 & w2_design_b$variables$Q37T==0])
nm3 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==3 & w2_design_b$variables$Q37T==0])
nm4 <- mean(w2_design_b$variables$Q40T[w2_design_b$variables$Q11==4 & w2_design_b$variables$Q37T==0])

mnb <- c(m1,m2,m3,m4,gap,nm1,nm2,nm3,nm4)
monthdat <- data.frame(cbind(ag,mnb))


# Create plot - Mil in last year
y1 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==1 & w2_design_c$variables$Q38T==1])
y2 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==2 & w2_design_c$variables$Q38T==1])
y3 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==3 & w2_design_c$variables$Q38T==1])
y4 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==4 & w2_design_c$variables$Q38T==1])
gap <- NA
ny1 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==1 & w2_design_c$variables$Q38T==0])
ny2 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==2 & w2_design_c$variables$Q38T==0])
ny3 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==3 & w2_design_c$variables$Q38T==0])
ny4 <- mean(w2_design_c$variables$Q40T[w2_design_c$variables$Q11==4 & w2_design_c$variables$Q38T==0])

mnc <- c(y1,y2,y3,y4,gap,ny1,ny2,ny3,ny4)
yeardat <- data.frame(cbind(ag,mnc))



# Create plot - Family in mil
f1 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==1 & w2_design_d$variables$Q39T==1])
f2 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==2 & w2_design_d$variables$Q39T==1])
f3 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==3 & w2_design_d$variables$Q39T==1])
f4 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==4 & w2_design_d$variables$Q39T==1])
gap <- NA
nf1 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==1 & w2_design_d$variables$Q39T==0])
nf2 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==2 & w2_design_d$variables$Q39T==0])
nf3 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==3 & w2_design_d$variables$Q39T==0])
nf4 <- mean(w2_design_d$variables$Q40T[w2_design_d$variables$Q11==4 & w2_design_d$variables$Q39T==0])

mnd <- c(f1,f2,f3,f4,gap,nf1,nf2,nf3,nf4)
famdat <- data.frame(cbind(ag,mnd))



par(mfrow=c(2,2))
b1<-barplot(mna, names.arg = ag, 
            xlab = 'Veteran                                                                                                           Civilian', 
            ylab='% Who Would Advise a Family Member to Join',
            ylim=c(0,1.01),
            col = c('gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40'))
text(b1, mna+0.025, labels=as.character(round(mna, digits=2)))

b2<-barplot(mnb, names.arg = ag, 
            xlab = 'Worked With Mil in Last Month                                                                        No Contact', 
            ylab='% Who Would Advise a Family Member to Join',
            ylim=c(0,1.01),
            col = c('gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40'))
text(b2, mnb+0.025, labels=as.character(round(mnb, digits=2)))

b3<-barplot(mnc, names.arg = ag, 
            xlab = 'Worked With Mil in Last Year                                                                                 No Contact', 
            ylab='% Who Would Advise a Family Member to Join',
            ylim=c(0,1.01),
            col = c('gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40'))
text(b3, mnc+0.025, labels=as.character(round(mnc, digits=2)))

b4<-barplot(mnd, names.arg = ag, 
            xlab = 'Family in Military                                                                                                     No Family in Military', 
            ylab='% Who Would Advise a Family Member to Join',
            ylim=c(0,1.01),
            col = c('gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40','gray40'))
text(b4, mnd+0.025, labels=as.character(round(mnd, digits=2)))